<template>
  <div class="Login">
    <div class="con">
      <div class="con_top">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="dl">
          <el-button type="primary" @click="submitForm('form')">登录</el-button>
          <el-button @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Result } from "element-ui";
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 5,
            message: "长度在 3 到 5 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(form) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$http.post("login", this.form).then((res) => {
            if (res.data.meta.status == 200) {
              this.$message({
                showClose: true,
                message: res.data.meta.msg,
                type: "success",
              });
              //浏览器会话的缓存，登录保存token
              window.sessionStorage.setItem("token", res.data.data.token);
              window.sessionStorage.setItem("username", res.data.data.username);
              this.$router.push("/home");
            } else {
              this.$message({
                showClose: true,
                message: res.data.meta.msg,
                type: "error",
              });
            }
          }).catch((err)=>{
              this.$message({
                showClose: true,
                message: "请求失败！",
                type: "error",
              });
          })
        }
      });
    },
    resetForm(form) {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.dl {
  float: right;
}
.el-form {
  width: 90%;
}
.Login {
  height: 100%;
  width: 100%;
  background-color: #1a4c6b;
}

.con {
  width: 450px;
  height: 300px;
  background-color: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.con_top {
  height: 130px;
  width: 130px;
  border-radius: 50%;
  border: 10px solid white;
  background-color: #eeeeee;
  overflow: hidden;
  position: absolute;
  top: -50%;
  left: 50%;
  transform: translate(-50%, 50%);
  box-shadow: 0 2px 10px rgba(114, 114, 114);
}

.con_top img {
  height: 130px;
}

.el-form {
  margin-top: 100px;
}

.login {
  float: right;
  margin-right: 38px;
}
</style>